package py.com.alfa.escriba.gui.rest.services.resttemplate;

import java.io.IOException;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;

import py.com.alfa.escriba.gui.config.EscribaConfigApplicationContextProvider;
import py.com.alfa.escriba.gui.dto.gestion.DatoscomplementariosDTO;
import py.com.alfa.escriba.gui.rest.services.DatosComplService;
import py.com.alfa.escriba.gui.services.Services;
import py.com.sif.rest.client.spring.request.ClientRequestBuilder;
@SuppressWarnings({ "unchecked", "unused"})
public class DatosComplServiceClient implements DatosComplService {
	
	private Logger log = LoggerFactory.getLogger(getClass());
	
	public DatosComplServiceClient() {}

	@Override
	public List<DatoscomplementariosDTO> listDatoscomplementarios() throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		ResponseEntity<DatoscomplementariosDTO[]> response = (ResponseEntity<DatoscomplementariosDTO[]>) request.exchange(LIST_DATOS, HttpMethod.GET, DatoscomplementariosDTO[].class);
		log.info("OK - listDatoscomplementarios ");
		List<DatoscomplementariosDTO> result = request.getArrayAsList(response.getBody());
		return result;
	}

	@Override
	public DatoscomplementariosDTO createDatoscomplementarios(DatoscomplementariosDTO datoscomplementarios)
			throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addBody(datoscomplementarios);
		ResponseEntity<DatoscomplementariosDTO> response = (ResponseEntity<DatoscomplementariosDTO>) request.exchange(LIST_DATOS, HttpMethod.POST, DatoscomplementariosDTO.class);
		log.info("OK - createDatoscomplementarios ");
		DatoscomplementariosDTO result = response.getBody();
		return result;
	}

	@Override
	public DatoscomplementariosDTO saveDatoscomplementarios(DatoscomplementariosDTO datoscomplementarios) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addParam("id", datoscomplementarios.getId().toString());
		request.addBody(datoscomplementarios);
		ResponseEntity<DatoscomplementariosDTO> response = (ResponseEntity<DatoscomplementariosDTO>) request.exchange(MB_DATOS, HttpMethod.PUT, DatoscomplementariosDTO.class);
		log.info("OK - saveDatoscomplementarios ");
		DatoscomplementariosDTO result = response.getBody();
		return result;
	}

	@Override
	public void deleteDatoscomplementarios(Long id) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addParam("id", id.toString());
		request.exchange(MB_DATOS, HttpMethod.DELETE, null);
		log.info("OK - deleteDatoscomplementarios ");

	}

	@Override
	public DatoscomplementariosDTO showDatoscomplementarios(Long id) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addParam("id", id.toString());
		ResponseEntity<DatoscomplementariosDTO> response = (ResponseEntity<DatoscomplementariosDTO>) request.exchange(MB_DATOS, HttpMethod.GET, DatoscomplementariosDTO.class);
		log.info("OK - showDatoscomplementarios ");
		DatoscomplementariosDTO result = response.getBody();
		return result;
	}

}
